/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.select;


import java.util.ArrayList;

import org.databasefacade.table.column.Column;


public class On
{
	Comparison onCompare = null;
	Condition conditions = new Condition();
	
	/*
	 * Used to build native sql
	 */
	public Comparison getOnCompare() {
		return onCompare;
	}
	
	public Condition getConditions() {
		return conditions;
	}
	
	/*
	 * Used to build statements
	 */
	public Condition on()
	{
		return conditions;
	}

	public Condition on(Column col1, Column col2)
	{
		Comparison compare = new Comparison();
		compare.equalToField(col1, col2);
		return on(compare);
	}

	public Condition on(Comparison compare)
	{
		onCompare = compare;
		return conditions;
	}

	public String getSQL()
	{
		if ( onCompare != null ) {
			return " ON " + onCompare.getSQL() + conditions.getSQL();
		} else {
			return " ON " + conditions.getSQL();
		}
	}
	
	public String getNativeSQL()
	{
		if ( onCompare != null ) {
			return " ON " + onCompare.getNativeSQL() + conditions.getNativeSQL();
		} else {
			return " ON " + conditions.getNativeSQL();
		}
	}

	public String toString()
	{
		return getSQL();
	}
	
	public ArrayList getAllComparisons() {
		ArrayList a = conditions.getAllComparisons();
		if ( onCompare != null ) {
			a.add(onCompare);
		}
		return a;
	}
}
